package com.test.mooan.labama.bean_boom.process_boom;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.test.mooan.labama.bean_boom.config_info_boom.AppShowInfo_boom;
import com.test.mooan.labama.utils_boom.LoanStatusUtil;
import com.test.mooan.labama.utils_boom.Stringutil_boom;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


public class ProcessInfo_boom {

    private static class Inner {
        private static ProcessInfo_boom instance = new ProcessInfo_boom();
    }

    private ProcessInfo_boom() {}

    public static ProcessInfo_boom getInstance() {
        return Inner.instance;
    }

    public static void parse(String jsonStr) {
        Gson gson = new Gson();
        Inner.instance = gson.fromJson(jsonStr, ProcessInfo_boom.class);
        try {
            Inner.instance.srcJSON = new JSONObject(jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //判断当前订单状态显示对应的界面
        if (LoanStatusUtil.isOrderPendingRepayment(Inner.instance.orderStatus)) {//是否在待还款-未逾期
            Inner.instance.sLoanStatus(LoanStatusUtil.LoanStatus.Order_Pending_Repayment);
        } else if (LoanStatusUtil.isOrderOverdueRepayment(Inner.instance.gOrderStatus())) {//是否在待还款-已逾期
            Inner.instance.sLoanStatus(LoanStatusUtil.LoanStatus.Order_Overdue_Repayment);
        } else if (LoanStatusUtil.isLoanReviewing(Inner.instance.gApplyStatus())) {//是否有审核中的贷款
            Inner.instance.sLoanStatus(LoanStatusUtil.LoanStatus.Loan_Reviewing);
        }  else if (LoanStatusUtil.isOrderLoaning(Inner.instance.gOrderStatus())) {//是否在待放款
            Inner.instance.sLoanStatus(LoanStatusUtil.LoanStatus.Order_Pending);
        } else if (LoanStatusUtil.isLoanRejcted(Inner.instance.gApplyStatus(), Inner.instance.gOrderStatus())) {//申请是否被拒绝
            Inner.instance.sLoanStatus(LoanStatusUtil.LoanStatus.Loan_Rejected);
        }else {
            Inner.instance.sLoanStatus(LoanStatusUtil.LoanStatus.No_Order);
        }

    }

    private String product;
    private ArrayList<String> allStepTitles;
    private ArrayList<String> allSteps;
    private ArrayList<String> steps;
    private LoanStatusUtil.LoanStatus loanStatus;
    private String idCard;
    private String idCardImg;
    private String birthDate;
    private String applyStatus;//申请订单状态
    private String orderStatus;//还款订单状态
    private String deniedBetweenDay;//被拒重新申请天数
    private String userType;
    private JSONObject srcJSON;

    private String smsFlag;
    private String whatsappFlag;
    private String telegramFlag;
    private String mobileEditFlag;
    private String telegramBotFlag;

    private String recipientName;

    private String holidayWindowFlag;
    private String hwDays;
    private String hwRate;

    //越南额外流程
    private ArrayList<String> extStep;
    private String ocr_info;

    public boolean gSmsFlag() {
        return "YES".equalsIgnoreCase(smsFlag) || "true".equalsIgnoreCase(smsFlag);
    }

    public boolean gWhatsappFlag() {
        return "YES".equalsIgnoreCase(whatsappFlag) || "true".equalsIgnoreCase(whatsappFlag);
    }

    public boolean gTelegramFlag() {
        return "YES".equalsIgnoreCase(telegramFlag) || "true".equalsIgnoreCase(telegramFlag);
    }

    public boolean gMobileEditFlag() {
        return "YES".equalsIgnoreCase(mobileEditFlag) || "true".equalsIgnoreCase(mobileEditFlag);
    }

    public boolean gTelegramBotFlag() {
        return "YES".equalsIgnoreCase(telegramBotFlag) || "true".equalsIgnoreCase(telegramBotFlag);
    }

    public boolean isNeedVerification(){
        if (AppShowInfo_boom.getInstance().isTestAccount()) return false;
        return gSmsFlag() || gWhatsappFlag() || gTelegramFlag() || gTelegramBotFlag();
    }

    public String gStepName(int index){
        if (steps==null||index<0||index>=steps.size()) return "";
        return steps.get(index);
    }

    public String gH5Url(){
        if (srcJSON==null) return "";
        return Stringutil_boom.getSafeString(srcJSON.optString("h5"));
    }

    public ProcessStep_boom gStep(int index){
        if (steps==null||index<0||index>=steps.size()||srcJSON==null) return null;
        String stepStr = srcJSON.optString(steps.get(index));

        return new Gson().fromJson(stepStr, ProcessStep_boom.class);
    }

    public ArrayList<String> gSteps() {
        return steps;
    }

    public void sLoanStatus(LoanStatusUtil.LoanStatus loanStatus) {
        this.loanStatus = loanStatus;
    }

    public LoanStatusUtil.LoanStatus gLoanStatus(){
        return loanStatus;
    }

    public String gOrderStatus() {
        return Stringutil_boom.getSafeString(orderStatus);
    }

    public String gApplyStatus() {
        return Stringutil_boom.getSafeString(applyStatus);
    }

    public String gDeniedBetweenDay() {
        if (TextUtils.isEmpty(deniedBetweenDay)) return "30";
        return Stringutil_boom.getSafeString(deniedBetweenDay);
    }

    public ArrayList<String> gAllSteps() {
        if (allSteps==null) return new ArrayList<>();
        return allSteps;
    }

    public String gRecipientName() {
        return Stringutil_boom.getSafeString(recipientName);
    }

    public boolean isNewUser(){
        return !Stringutil_boom.getSafeString(userType).equalsIgnoreCase("OLD");
    }

    public ArrayList<String> gAllStepTitles() {
        if (allStepTitles==null) return new ArrayList<>();
        return allStepTitles;
    }

    public String gStepTitle(){
        if (allStepTitles==null||allStepTitles.size()<allSteps.size()) return "";
        String stepName = gStepName(0);
        int i = allSteps.indexOf(stepName);
        if (i<0||i>=allStepTitles.size()) return "";
        return allStepTitles.get(i);
    }

    public boolean showHolidayWindowFlag(){
        return "YES".equalsIgnoreCase(holidayWindowFlag) || "true".equalsIgnoreCase(holidayWindowFlag);
    }

    public String gHwDays(){
        return Stringutil_boom.getSafeString(hwDays);
    }

    public String gHwRate(){
        return Stringutil_boom.getSafeString(hwRate);
    }

}
